﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package Classes.Managers {
    import flash.events.*;
    import Engine.Managers.*;
    import Engine.Helpers.*;
    import Display.DialogUI.*;
    import Classes.*;
    import Transactions.*;
    import Classes.util.*;
    import Display.*;
    import Classes.doobers.*;
    import Classes.actions.*;
    import Init.PostInit.PostInitActions.*;
    import Engine.*;

    public class InGameXPromoManager {

        protected static var m_hasInitted:Boolean = false;
        protected static var m_instance:InGameXPromoManager = null;

        protected var m_xpromoFriend:XPromoFriend = null;

        public function InGameXPromoManager(){
            if (InGameXPromoManager.m_instance != null){
                throw (new Error("Attempting to instantiate more than one InGameXPromoManager"));
            };
        }
        protected static function initialize():void{
            if (!InGameXPromoManager.m_instance){
                m_instance = new (InGameXPromoManager)();
            } else {
                GlobalEngine.log("XPromo", "Attempting to initialize xpromos after the manager has been instantiated");
            };
        }
        public static function getInstance():InGameXPromoManager{
            if (!InGameXPromoManager.m_instance){
                InGameXPromoManager.initialize();
            };
            return (m_instance);
        }
        public static function checkXPromoGates(_arg1:Object):Boolean{
            var _local3:Number;
            var _local4:Number;
            var _local2:Boolean;
            if (((_arg1.hasOwnProperty("startDate")) && (_arg1.hasOwnProperty("endDate")))){
                _local3 = GameUtil.synchronizedDateToNumber(_arg1.startDate);
                _local4 = GameUtil.synchronizedDateToNumber(_arg1.endDate);
                if ((((_local3 <= GlobalEngine.getTimer())) && ((_local4 >= GlobalEngine.getTimer())))){
                    if (((_arg1.hasOwnProperty("level")) && ((int(_arg1.level) <= Global.player.level)))){
                        _local2 = true;
                        if (_arg1.hasOwnProperty("experiment")){
                            _local2 = (Global.experimentManager.getVariant(_arg1.experiment) > 0);
                        };
                    };
                };
            };
            return (_local2);
        }

        public function getStartupDialog():StartupDialogBase{
            var _local2:Object;
            var _local3:Boolean;
            var _local4:Function;
            var _local5:String;
            var _local6:String;
            var _local7:SamAdviceDialog;
            var _local8:StartupDialogBase;
            var _local1:Array = Global.gameSettings.getXpromos();
            for each (_local2 in _local1) {
                _local3 = InGameXPromoManager.checkXPromoGates(_local2);
                if (((((_local3) && (_local2.startUpDialog))) && (!(Global.player.getSeenFlag(("xpromo_" + _local2.nameKey)))))){
                    GameTransactionManager.addTransaction(new TSetXPromoSeen(_local2));
                    this.m_xpromoFriend = new XPromoFriend(_local2);
                    _local4 = null;
                    if (m_instance[("on" + _local2.nameKey)]){
                        _local4 = (m_instance[("on" + _local2.nameKey)] as Function);
                    };
                    _local5 = ZLoc.t("Dialogs", ("xpromo_advice_" + _local2.nameKey));
                    _local6 = ZLoc.t("Dialogs", (("xpromo_advice_" + _local2.nameKey) + "_title"));
                    _local7 = new SamAdviceDialog(_local5, "", GenericPopup.TYPE_OK, _local4, "", "", true, 0, "Okay");
                    _local8 = new StartupDialogBase(_local7, "d2_invitefriendsdialog", false);
                    StatsManager.count("xpromo", _local2.nameKey, "dialog_startup");
                    return (_local8);
                };
            };
            return (null);
        }
        public function onFarmEngland(_arg1:Event=null):void{
            if (_arg1){
                _arg1.stopImmediatePropagation();
                _arg1.stopPropagation();
            };
            var _local2:int = Global.gameSettings.getInt("startCameraX");
            var _local3:int = Global.gameSettings.getInt("startCameraY");
            var _local4:Vector2 = IsoMath.screenPosToTilePos((GlobalEngine.viewport.x + GlobalEngine.viewport.width), (GlobalEngine.viewport.y + GlobalEngine.viewport.height));
            var _local5:Vector3 = new Vector3(_local4.x, _local4.y);
            var _local6:Vehicle = new Vehicle("npc_fvairship", false);
            var _local7:Vector2 = IsoMath.screenPosToTilePos(GlobalEngine.viewport.x, (GlobalEngine.viewport.y - 50));
            var _local8:Vector3 = new Vector3(_local7.x, _local7.y);
            var _local9:Vector2 = new Vector2(_local2, _local3);
            var _local10:Vector3 = new Vector3(_local9.x, _local9.y);
            var _local11:Array = [];
            _local11.push(new ActionNavigateBeeline(_local6, _local10, _local8));
            _local11.push(new ActionPause(_local6, 1));
            _local11.push(new ActionFn(_local6, this.onDropOff));
            _local11.push(new ActionPause(_local6, 1));
            _local11.push(new ActionNavigateBeeline(_local6, _local5, _local10));
            _local11.push(new ActionDie(_local6));
            _local6 = Global.world.citySim.npcManager.addFlyerByName(_local6, _local8, _local11);
            _local6.velocityWalk = 2.5;
            _local6.velocityRun = 2.5;
        }
        public function onDropOff(_arg1:Event=null):void{
            var _local2:int = Global.gameSettings.getInt("startCameraX");
            var _local3:int = Global.gameSettings.getInt("startCameraY");
            var _local4:Array = [];
            var _local5:int = this.m_xpromoFriend.configObj.numDoobers;
            var _local6:int = this.m_xpromoFriend.configObj.goods;
            while ((((_local6 > 0)) && ((_local5 > 0)))) {
                _local4.push([Global.gameSettings.getDooberFromType(Doober.DOOBER_GOODS, _local6), _local6]);
                _local5--;
            };
            Global.world.dooberManager.createBatchDoobers(_local4, null, _local2, _local3, false, this.onDoobersCollected);
        }
        public function onGagaville(_arg1:Event=null):void{
            if (_arg1){
                _arg1.stopImmediatePropagation();
                _arg1.stopPropagation();
            };
        }
        public function onGaGaFarm(_arg1:Event=null):void{
            if (_arg1){
                _arg1.stopImmediatePropagation();
                _arg1.stopPropagation();
            };
        }
        public function onDoobersCollected(_arg1:MouseEvent=null):void{
            this.m_xpromoFriend.onXPromo(_arg1);
        }

    }
}//package Classes.Managers 
